package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.system.config.domain.Config;
import com.abvnet.project.system.config.service.IConfigService;
import com.abvnet.project.water.project.domain.*;
import com.abvnet.project.water.project.service.IProjectInfoService;
import com.abvnet.project.water.project.service.IProjectLogService;
import com.abvnet.project.water.project.service.IProjectWaterService;
import com.abvnet.project.water.project.service.WordGeneratorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/project/projectData")
public class ProjectDataController extends BaseController {

    private String prefix = "project/projectData";

    @Autowired
    private IProjectInfoService projectInfoService;

    @Autowired
    private IProjectLogService projectLogService;

    @Autowired
    private IProjectWaterService projectWaterService;

    @Autowired
    private IConfigService configService;

    @RequiresPermissions("project:projectData:list")
    @GetMapping()
    public String projectData()
    {
        return prefix + "/list";
    }

    @RequiresPermissions("project:projectData:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectInfo projectInfo) {
        if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
            projectInfo.setCreateBy(ShiroUtils.getLoginName());
            projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
        }
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
    }

    @GetMapping("/data")
    public String data(ProjectLog projectLog, ModelMap mmap) {
        // 进出水，水质，日期
        WaterChart waterChart = projectLogService.selectProjectLogListForData(projectLog);
        MicroBar microBar = projectLogService.selectProjectLogMicroListForData(projectLog);
        ProjectWater waterStandard = projectWaterService.selectProjectWaterStandardByProjectId(projectLog.getProjectId());
        ProjectInfo projectInfo = projectInfoService.selectProjectInfoById(projectLog.getProjectId());
        mmap.put("projectInfo", projectInfo);
        mmap.put("microBar", microBar);
        mmap.put("waterChart", waterChart);
        if (waterStandard != null) {
            mmap.put("standardCod", waterStandard.getStandardCod() == null ? -1 : waterStandard.getStandardCod());
            mmap.put("standardNh3N", waterStandard.getStandardNh3N() == null ? -1 : waterStandard.getStandardNh3N());
            mmap.put("standardTotalN", waterStandard.getStandardTotalN() == null ? -1 : waterStandard.getStandardTotalN());
            mmap.put("standardTotalP", waterStandard.getStandardTotalP() == null ? -1 : waterStandard.getStandardTotalP());
            mmap.put("standardPh", waterStandard.getStandardPh() == null ? -1 : waterStandard.getStandardPh());
            mmap.put("standardDo", waterStandard.getStandardDo() == null ? -1 : waterStandard.getStandardDo());
            mmap.put("standardT", waterStandard.getStandardT() == null ? -1 : waterStandard.getStandardT());
        } else {
            mmap.put("standardCod", -1);
            mmap.put("standardNh3N", -1);
            mmap.put("standardTotalN", -1);
            mmap.put("standardTotalP", -1);
            mmap.put("standardPh", -1);
            mmap.put("standardDo", -1);
            mmap.put("standardT", -1);
        }
        return prefix + "/data";
    }

    /**
     * 导出数据统计
     * @param projectSummary
     * @return
     */
    @PostMapping("/export")
    public void export(ProjectSummary projectSummary, HttpServletRequest request, HttpServletResponse response) {
        try {
            WordGeneratorUtils.exportMillCertificateWord("dataTemplate.ftl", request, response, projectSummary);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Log(title = "项目总结/数据统计模板", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(@RequestParam("type") String type, @RequestParam("templateFile") MultipartFile templateFile) {
        try {
            if (templateFile != null && !templateFile.isEmpty()) {
                String fileName = "summaryTemplate";
                if (!type.equals("summary"))
                    fileName = "dataTemplate";
                FileUploadUtils.upload(AbvnetConfig.getTemplatePath(),fileName,
                            templateFile);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            //log.error("上传项目总结/数据统计失败！", e);
            return error(e.getMessage());
        }
    }

    @GetMapping("/projectArea")
    public String projectArea(ModelMap mmap) {
        return "main_v2";
    }
}